home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 97 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.0 KB

  1. Path: solon.com!not-for-mail
  2. From: ig25@mvmap66.ciw.uni-karlsruhe.de (Thomas Koenig)
  3. Newsgroups: comp.std.c,comp.lang.c.moderated
  4. Subject: Re: Help, best way to compare doubles
  5. Followup-To: comp.lang.c.moderated
  6. Date: 13 Jan 1996 11:53:29 -0600
  7. Organization: =?ISO-8859-1?Q?Universit=E4t_Karlsruhe_(TH),_Germany_?=
  8. Sender: clc@solutions.solon.com
  9. Approved: clc@solutions.solon.com
  10. Message-ID: <4d8rip$bk2@solutions.solon.com>
  11. References: <4d1k09$aqq@mercury.IntNet.net> <4d8i3o$pjb@info.uah.edu>
  12. Reply-To: Thomas.Koenig@ciw.uni-karlsruhe.de
  13. NNTP-Posting-Host: solutions.solon.com
  14.  
  15. [ Followup-To: comp.lang.c.moderated ]
  16.  
  17. In comp.std.c, gbacon@oreo (Greg Bacon) wrote:
  18. >Jeff Tomich (jtomich@IntNet.net) wrote:
  19. >: Having a hard time to figure out a function on how to compare doubles. 
  20. >: Any ideas?
  21.  
  22. >No function necessary.. C has this really cool boolean equality operator
  23. >that does this sort of thing for you.  Have a look:
  24.  
  25. [...]
  26.  
  27. It is unwise to compare floating point numbers for equality without
  28. thinking hard first.  Roundoff error means that mathematically
  29. equivalent expressions may yield different results for different
  30. orderings; for example, a+b+c may very well compare unequal to
  31. a+c+b (try this out with a=1.0, b=-1.0 and c=1.E-20, for example).
  32. Roundoff error can also mean that algorithms which should converge
  33. don't, so beware.  (I even know one system where a/2*2 != a for one
  34. quarter of floating point numbers - no points for guessing it's
  35. a /370 :-)
  36.  
  37. The correct way to compare two floating point numbers for "equality" to
  38. use fabs(a-b) < eps.  The problem arises in determining what to use for
  39. eps, of course :-).  What's reasonable depends very much on your problem
  40. and on the floating point characteristics of your system (for example,
  41. do your values span many orders of magnitude?  Are they close to unity?
  42. Could they have opposing signs?), so it's really impossible to tell
  43. without a fairly thorough analysis.
  44. -- 
  45. Thomas Koenig, Thomas.Koenig@ciw.uni-karlsruhe.de, ig25@dkauni2.bitnet.
  46. The joy of engineering is to find a straight line on a double
  47. logarithmic diagram.
  48.